﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using AutoR.Helpers;
using AutoR.Messages;
using AutoR.Models;
using AutoR.Services;
using Prism.Mvvm;
using Xamarin.Forms;

namespace AutoR.Tasks
{
	// Token: 0x0200010D RID: 269
	public class StrengthenTask : BindableBase
	{
		// Token: 0x1700018E RID: 398
		// (get) Token: 0x060005BA RID: 1466 RVA: 0x000408F6 File Offset: 0x0003EAF6
		protected GameData Game
		{
			get
			{
				return Runtime.Game;
			}
		}

		// Token: 0x1700018F RID: 399
		// (get) Token: 0x060005BB RID: 1467 RVA: 0x00057E9A File Offset: 0x0005609A
		// (set) Token: 0x060005BC RID: 1468 RVA: 0x00057EA4 File Offset: 0x000560A4
		public int Id
		{
			[CompilerGenerated]
			get
			{
				return this.<Id>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<Id>k__BackingField == value)
				{
					return;
				}
				this.<Id>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Id);
			}
		}

		// Token: 0x17000190 RID: 400
		// (get) Token: 0x060005BD RID: 1469 RVA: 0x00057ED1 File Offset: 0x000560D1
		// (set) Token: 0x060005BE RID: 1470 RVA: 0x00057EDC File Offset: 0x000560DC
		public string Type
		{
			[CompilerGenerated]
			get
			{
				return this.<Type>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (string.Equals(this.<Type>k__BackingField, value, StringComparison.Ordinal))
				{
					return;
				}
				this.<Type>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Type);
			}
		}

		// Token: 0x17000191 RID: 401
		// (get) Token: 0x060005BF RID: 1471 RVA: 0x00057F0D File Offset: 0x0005610D
		// (set) Token: 0x060005C0 RID: 1472 RVA: 0x00057F18 File Offset: 0x00056118
		public bool Enabled
		{
			[CompilerGenerated]
			get
			{
				return this.<Enabled>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<Enabled>k__BackingField == value)
				{
					return;
				}
				this.<Enabled>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Enabled);
			}
		}

		// Token: 0x17000192 RID: 402
		// (get) Token: 0x060005C1 RID: 1473 RVA: 0x00057F45 File Offset: 0x00056145
		// (set) Token: 0x060005C2 RID: 1474 RVA: 0x00057F50 File Offset: 0x00056150
		public bool IsSkillLevelUp
		{
			[CompilerGenerated]
			get
			{
				return this.<IsSkillLevelUp>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<IsSkillLevelUp>k__BackingField == value)
				{
					return;
				}
				this.<IsSkillLevelUp>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.IsSkillLevelUp);
			}
		}

		// Token: 0x17000193 RID: 403
		// (get) Token: 0x060005C3 RID: 1475 RVA: 0x00057F7D File Offset: 0x0005617D
		// (set) Token: 0x060005C4 RID: 1476 RVA: 0x00057F88 File Offset: 0x00056188
		public bool IsStrengthen
		{
			[CompilerGenerated]
			get
			{
				return this.<IsStrengthen>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<IsStrengthen>k__BackingField == value)
				{
					return;
				}
				this.<IsStrengthen>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.IsStrengthen);
			}
		}

		// Token: 0x17000194 RID: 404
		// (get) Token: 0x060005C5 RID: 1477 RVA: 0x00057FB5 File Offset: 0x000561B5
		// (set) Token: 0x060005C6 RID: 1478 RVA: 0x00057FC0 File Offset: 0x000561C0
		public bool IsDismantleBoat
		{
			[CompilerGenerated]
			get
			{
				return this.<IsDismantleBoat>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<IsDismantleBoat>k__BackingField == value)
				{
					return;
				}
				this.<IsDismantleBoat>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.IsDismantleBoat);
			}
		}

		// Token: 0x17000195 RID: 405
		// (get) Token: 0x060005C7 RID: 1479 RVA: 0x00057FED File Offset: 0x000561ED
		// (set) Token: 0x060005C8 RID: 1480 RVA: 0x00057FF8 File Offset: 0x000561F8
		public int DismantleStar
		{
			[CompilerGenerated]
			get
			{
				return this.<DismantleStar>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<DismantleStar>k__BackingField == value)
				{
					return;
				}
				this.<DismantleStar>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.DismantleStar);
			}
		}

		// Token: 0x17000196 RID: 406
		// (get) Token: 0x060005C9 RID: 1481 RVA: 0x00058025 File Offset: 0x00056225
		// (set) Token: 0x060005CA RID: 1482 RVA: 0x00058030 File Offset: 0x00056230
		public int DismantleLevel
		{
			[CompilerGenerated]
			get
			{
				return this.<DismantleLevel>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<DismantleLevel>k__BackingField == value)
				{
					return;
				}
				this.<DismantleLevel>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.DismantleLevel);
			}
		}

		// Token: 0x17000197 RID: 407
		// (get) Token: 0x060005CB RID: 1483 RVA: 0x0005805D File Offset: 0x0005625D
		// (set) Token: 0x060005CC RID: 1484 RVA: 0x00058068 File Offset: 0x00056268
		public bool IsKeepEequipment
		{
			[CompilerGenerated]
			get
			{
				return this.<IsKeepEequipment>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<IsKeepEequipment>k__BackingField == value)
				{
					return;
				}
				this.<IsKeepEequipment>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.IsKeepEequipment);
			}
		}

		// Token: 0x17000198 RID: 408
		// (get) Token: 0x060005CD RID: 1485 RVA: 0x00058095 File Offset: 0x00056295
		// (set) Token: 0x060005CE RID: 1486 RVA: 0x000580A0 File Offset: 0x000562A0
		public string DisWhitelist
		{
			[CompilerGenerated]
			get
			{
				return this.<DisWhitelist>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (string.Equals(this.<DisWhitelist>k__BackingField, value, StringComparison.Ordinal))
				{
					return;
				}
				this.<DisWhitelist>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.DisWhitelist);
			}
		}

		// Token: 0x17000199 RID: 409
		// (get) Token: 0x060005CF RID: 1487 RVA: 0x000580D1 File Offset: 0x000562D1
		// (set) Token: 0x060005D0 RID: 1488 RVA: 0x000580DC File Offset: 0x000562DC
		public bool IsDismantleEequipment
		{
			[CompilerGenerated]
			get
			{
				return this.<IsDismantleEequipment>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<IsDismantleEequipment>k__BackingField == value)
				{
					return;
				}
				this.<IsDismantleEequipment>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.IsDismantleEequipment);
			}
		}

		// Token: 0x1700019A RID: 410
		// (get) Token: 0x060005D1 RID: 1489 RVA: 0x00058109 File Offset: 0x00056309
		// (set) Token: 0x060005D2 RID: 1490 RVA: 0x00058114 File Offset: 0x00056314
		public ObservableCollection<int> StrengthenShip
		{
			[CompilerGenerated]
			get
			{
				return this.<StrengthenShip>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (object.Equals(this.<StrengthenShip>k__BackingField, value))
				{
					return;
				}
				this.<StrengthenShip>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.StrengthenShip);
			}
		}

		// Token: 0x060005D3 RID: 1491 RVA: 0x00058144 File Offset: 0x00056344
		public async Task Start()
		{
			if (this.IsDismantleEequipment)
			{
				List<UserEquipment> list2 = (from i in this.Game.Equipments
				where i.locked == 0 && i.num > 0 && i.Star < 3 && i.equipmentCid != 1
				select i).ToList<UserEquipment>();
				if ((from i in list2
				select i.num).Sum() > 10)
				{
					foreach (UserEquipment equipment in list2)
					{
						List<UserEquipment> update = await Runtime.Remote.DismantleEquipment(equipment.equipmentCid, equipment.num);
						MessagingCenter.Send<LogMessage>(new LogMessage(string.Format("[分解]废弃{0}共{1}个成功", equipment.Name, equipment.num)), "Log");
						this.Game.Equipments.UpdateEquipments(update);
						await this.Game.Delay(1000);
						equipment = null;
					}
					List<UserEquipment>.Enumerator enumerator = default(List<UserEquipment>.Enumerator);
				}
			}
			if (this.IsSkillLevelUp)
			{
				foreach (UserShip ship2 in (from i in this.Game.UserShip
				where i.isLocked && i.CanSkillLevelUp
				select i).ToList<UserShip>())
				{
					if (ship2.shipCid != 10030911)
					{
						SkillShip skillShip = await Runtime.Remote.SkillLevelUp(ship2.id);
						MessagingCenter.Send<LogMessage>(new LogMessage(string.Format("[强化]自动升级{0}技能成功,当前技能等级{1}", ship2.Name, skillShip.shipVO.skillLevel)), "Log");
						this.Game.UserShip.UpdateUserShip(new List<UserShip>
						{
							skillShip.shipVO
						});
					}
					ship2 = null;
				}
				List<UserShip>.Enumerator enumerator2 = default(List<UserShip>.Enumerator);
			}
			if (this.Game.User.shipNumTop - this.Game.UserShip.Count < 5)
			{
				List<UserShip> list = (from i in this.Game.UserShip
				where !i.isLocked && (i.fleetId == 0 & i.level < 10)
				select i).ToList<UserShip>();
				if (!string.IsNullOrEmpty(this.DisWhitelist))
				{
					List<string> source = this.DisWhitelist.Split(new char[]
					{
						'|'
					}).ToList<string>();
					using (List<UserShip>.Enumerator enumerator3 = list.ToList<UserShip>().GetEnumerator())
					{
						while (enumerator3.MoveNext())
						{
							UserShip ship = enumerator3.Current;
							if (source.Any((string i) => !string.IsNullOrEmpty(i) && ship.Name.Contains(i)))
							{
								list.Remove(ship);
							}
						}
					}
				}
				if (this.DismantleStar > 0)
				{
					list = (from i in list
					where i.Star < 6 - this.DismantleStar
					select i).ToList<UserShip>();
				}
				list = (from i in list
				where i.level <= this.DismantleLevel
				select i).ToList<UserShip>();
				bool streng = true;
				if (this.IsStrengthen)
				{
					List<UserShip> collection = (from i in this.Game.UserShip
					where !i.IsInExplore && i.isLocked && i.IsEvo && i.IsNeedStrengthen
					orderby i.level descending
					select i).ToList<UserShip>();
					List<UserShip> list3 = (from i in this.StrengthenShip.ToUserShipList()
					where i != null && !i.IsInExplore && i.IsNeedStrengthen
					select i).ToList<UserShip>();
					list3.AddRange(collection);
					using (List<UserShip>.Enumerator enumerator2 = list3.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							UserShip ship = enumerator2.Current;
							if (list.Count == 0)
							{
								break;
							}
							List<UserShip> list4 = new List<UserShip>();
							int num = 0;
							Func<UserShip, bool> <>9__11;
							Func<UserShip, int> <>9__12;
							Func<UserShip, int> <>9__13;
							while (num < list.Count && ship.IsNeedStrengthen)
							{
								IEnumerable<UserShip> source2 = list;
								Func<UserShip, bool> predicate;
								if ((predicate = <>9__11) == null)
								{
									Func<UserShip, bool> func = (UserShip i) => this.GetEffectiveValue(ship, i) > 0;
									<>9__11 = func;
									predicate = func;
								}
								IEnumerable<UserShip> source3 = source2.Where(predicate);
								Func<UserShip, int> keySelector;
								if ((keySelector = <>9__12) == null)
								{
									Func<UserShip, int> func2 = (UserShip i) => this.GetEffectiveValue(ship, i);
									<>9__12 = func2;
									keySelector = func2;
								}
								IOrderedEnumerable<UserShip> source4 = source3.OrderByDescending(keySelector);
								Func<UserShip, int> keySelector2;
								if ((keySelector2 = <>9__13) == null)
								{
									Func<UserShip, int> func3 = (UserShip i) => this.GetWasteValue(ship, i) + i.DismantleNum;
									<>9__13 = func3;
									keySelector2 = func3;
								}
								UserShip userShip = source4.ThenBy(keySelector2).FirstOrDefault<UserShip>();
								if (userShip != null)
								{
									list4.Add(userShip);
									list.Remove(userShip);
									ship.StrengthenShip(userShip);
								}
								num++;
							}
							if (list4.Count > 0)
							{
								streng = false;
								await this.Game.Strengthen(ship, list4);
							}
						}
					}
					List<UserShip>.Enumerator enumerator2 = default(List<UserShip>.Enumerator);
				}
				if (this.IsDismantleBoat && streng && list.Count > 0)
				{
					await this.Game.DismantleBoat((from i in list
					select i.id).ToList<int>(), Convert.ToInt32(!this.IsKeepEequipment));
				}
				list = null;
			}
		}

		// Token: 0x060005D4 RID: 1492 RVA: 0x0005818C File Offset: 0x0005638C
		private int GetEffectiveValue(UserShip source, UserShip eat)
		{
			int num = (source.AirDefSTR - eat.Strengthen.air_def > 0) ? eat.Strengthen.air_def : source.AirDefSTR;
			int num2 = (source.AtkSTR - eat.Strengthen.atk > 0) ? eat.Strengthen.atk : source.AtkSTR;
			int num3 = (source.DefSTR - eat.Strengthen.def > 0) ? eat.Strengthen.def : source.DefSTR;
			int num4 = (source.TorpedoSTR - eat.Strengthen.torpedo > 0) ? eat.Strengthen.torpedo : source.TorpedoSTR;
			return num + num2 + num3 + num4;
		}

		// Token: 0x060005D5 RID: 1493 RVA: 0x00058244 File Offset: 0x00056444
		private int GetWasteValue(UserShip source, UserShip eat)
		{
			int num = (eat.Strengthen.air_def - source.AirDefSTR > 0) ? (eat.Strengthen.air_def - source.AirDefSTR) : 0;
			int num2 = (eat.Strengthen.atk - source.AtkSTR > 0) ? (eat.Strengthen.atk - source.AtkSTR) : 0;
			int num3 = (eat.Strengthen.def - source.DefSTR > 0) ? (eat.Strengthen.def - source.DefSTR) : 0;
			int num4 = (eat.Strengthen.torpedo - source.TorpedoSTR > 0) ? (eat.Strengthen.torpedo - source.TorpedoSTR) : 0;
			return num + num2 + num3 + num4;
		}

		// Token: 0x060005D6 RID: 1494 RVA: 0x00058302 File Offset: 0x00056502
		public StrengthenTask()
		{
			this.<Id>k__BackingField = 6;
			this.<Type>k__BackingField = "StrengthenTask";
			this.<DismantleLevel>k__BackingField = 1;
			this.<StrengthenShip>k__BackingField = new ObservableCollection<int>();
			base..ctor();
		}
	}
}
